package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.api.req.InvestorQueryReq;
import com.mz.mattress.admin.api.resp.BonusSum;
import com.mz.mattress.admin.api.resp.IntegerCount;
import com.mz.mattress.admin.api.resp.InvestorSum;
import com.mz.mattress.admin.mapper.*;
import com.mz.mattress.admin.service.InvestorService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.MemberMer;
import com.mz.mattress.api.entity.Investor;
import com.mz.mattress.api.enums.MerchantStatusEnum;
import com.mz.mattress.api.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class InvestorServiceImpl implements InvestorService {

    @Autowired
    private InvestorMapper investorMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private MemberMerMapper memberMerMapper;

    @Autowired
    private OrderUseTimesBonusMapper orderUseTimesBonusMapper;

    @Override
    public Result<IPage<Investor>> listPage(InvestorQueryReq req) {
        Page page = req.newPage();
        Page<Investor> list = investorMapper.listPage(page,
                req.getName(), req.getStatus(), req.getStartTime(), req.getEndTime());
        return Result.ok(list);
    }

    @Override
    public Result<Investor> getById(Integer id) {

        if (id <= 0) {
            return Result.fail("投资商不存在");
        }

        Investor investor = investorMapper.selectById(id);
        if (investor == null) {
            return Result.fail("投资商不存在");
        }

//        InvestorSum investorSum = getInvestorSum(investor);
//        investor.setMerchantSum(investorSum.getMerchantSum());
//        investor.setBonusSum(investorSum.getBonusSum());

        return Result.ok(investor);
    }

    @Override
    public Result add(Investor investor) {

        if (StringUtils.isEmpty(investor.getMobile())) {
            return Result.fail("请输入手机号");
        }

        if (StringUtils.isEmpty(investor.getName())) {
            return Result.fail("请输入姓名");
        }

        String mobile = investor.getMobile();

        Investor one = investorMapper.selectByMobile(mobile);
        if (one != null) {
            return Result.fail("手机号已存在");
        }

        //绑定会员信息
        MemberMer memberMer = memberMerMapper.selectByMobile(mobile);
        if (memberMer != null) {
            investor.setMemberId(memberMer.getId());
        }
        investorMapper.insert(investor);

        return Result.ok();
    }

    @Override
    public Result updateById(Investor investor) {

        // 手机号不能改
        investor.setMobile(null);
        investor.setMemberId(null);
        investorMapper.updateById(investor);

        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        investorMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    @Override
    public Result<List<Investor>> list() {
        List<Investor> list = investorMapper.list();
        return Result.ok(list);
    }

    @Override
    public Result<InvestorSum> headSum(Integer id) {

        Investor investor = investorMapper.selectById(id);
        if (investor == null) {
            return Result.fail("投资商不存在");
        }

        InvestorSum investorSum = getInvestorSum(investor);

        return Result.ok(investorSum);
    }

    @Override
    public Result<BonusSum> investorSum(Integer typeId, Long memberMerId, Integer days) {

        days = days == null ? 0 : -days;
        String startTime = DateUtil.getNowDayPlus(days);

        BonusSum bonusSum = orderUseTimesBonusMapper.selectBonusSum(4, typeId, null, startTime);
        return Result.ok(bonusSum);
    }

    //实时统计
    @NotNull
    private InvestorSum getInvestorSum(Investor investor) {

        Integer id = investor.getId();

        InvestorSum investorSum = new InvestorSum();
        List<IntegerCount> merchantCount = merchantMapper.selectSumByColumn("investor_id", id);

        int pass = 0;
        for (IntegerCount statusCount : merchantCount) {
            if (statusCount.getStatus() >= MerchantStatusEnum.CHECK_PASS.getValue()) {
                pass += statusCount.getCount();
            }
        }
        investorSum.setMerchantSum(pass);

        List<IntegerCount> deviceCount = deviceMapper.selectSumByColumn("investor_id", id);

        int total = 0;
        for (IntegerCount statusCount : deviceCount) {
            total += statusCount.getCount();
        }
        investorSum.setDeviceSum(total);

        //类型 0平台 1商家 2销售 3子销售 4投资商
        int type = 4;

        investorSum.setBonusRatio(investor.getBonusRatio());

        MemberMer memberMer = memberMerMapper.selectById(investor.getMemberId());
        if (memberMer != null) {
            investorSum.setBonusSum(memberMer.getSumIncome());
            investorSum.setBonusOut(memberMer.getSumCashOut());
        }

        return investorSum;
    }

    @Override
    public void doSum() {

        List<Investor> list = investorMapper.list();

        for (Investor investor : list) {

            InvestorSum investorSum = getInvestorSum(investor);

            Investor update = new Investor();
            update.setId(investor.getId());
            update.setMerchantSum(investorSum.getMerchantSum());
            update.setDeviceSum(investorSum.getDeviceSum());
            investorMapper.updateById(update);

        }


    }


}
